<?php

/**
 * 服务器监听用户动作
 */

$serv->on('Message', function($server, $frame) {

    //推送用户在线列表
    if ($frame->data == 'getonlines') {
        foreach ($GLOBALS['fds'] as $v) {
            $server->push($v, 'pulllist'.json_encode($GLOBALS['userlist']));
        }

    //新进用户加入列表
    } else if (substr($frame->data, 0, 8) == 'mystatus') {

        $info = json_decode(substr($frame->data, 8));
        $GLOBALS['userlist'][$frame->fd] = $info;

    //推送大厅座位情况
    } else if (substr($frame->data, 0, 10) == 'tableinfos') {
        
        foreach ($GLOBALS['fds'] as $v) {
            $server->push($v, 'pulltables'.json_encode($GLOBALS['hall']));
        }

    //推送是否允许坐下
    } else if (substr($frame->data, 0, 8) == 'seatdown') {
        
        $info = json_decode(substr($frame->data, 8));
        if ($GLOBALS['hall'][$info->table][$info->seat] == 'off') {
            $server->push($frame->fd, 'seatdown'.'ok');
        } else {
            $server->push($frame->fd, 'seatdown'.'no');
        }

    //将加入房间的玩家信息更新到数组
    } else if (substr($frame->data, 0, 11) == 'getroominfo') {
        
        //写入用户状态信息
        $info = json_decode(substr($frame->data, 11));
        $GLOBALS['hall'][$info->roomid][$info->seatid] = $frame->fd;
        $GLOBALS['hall'][$info->roomid][$info->seatid.'_nick'] = $info->user_nick;
        $GLOBALS['hall'][$info->roomid][$info->seatid.'_status'] = $info->mystatus;
        //如果房间数组不存在，初始化
        if (!isset($GLOBALS['rooms'][$info->roomid])) {
            $GLOBALS['rooms'][$info->roomid] = [];
        }

        
        //判断是重新开局还是新进玩家
        $isreopen = false;
        foreach ($GLOBALS['rooms'][$info->roomid] as $v) {
            if ($v['fd'] == $frame->fd) {
                $isreopen = true;
                break;
            }            
        }

        //判断加入房间的是第几个 并加入游戏顺序
        if ($isreopen == false) {
            if (count($GLOBALS['rooms'][$info->roomid]) == 0) {
                $GLOBALS['rooms'][$info->roomid][0] = ['fd'=>$frame->fd,'nick'=>$info->user_nick];
            } else if (count($GLOBALS['rooms'][$info->roomid]) == 1) {
                $GLOBALS['rooms'][$info->roomid][1] = ['fd'=>$frame->fd,'nick'=>$info->user_nick];
            } else if (count($GLOBALS['rooms'][$info->roomid]) == 2) {
                $GLOBALS['rooms'][$info->roomid][2] = ['fd'=>$frame->fd,'nick'=>$info->user_nick];
            }
        }

        //广播到该房间所有的用户 广播到全局
        $fd1 = $GLOBALS['hall'][$info->roomid]['user_a'];
        $fd2 = $GLOBALS['hall'][$info->roomid]['user_b'];
        $fd3 = $GLOBALS['hall'][$info->roomid]['user_c'];
        foreach ($GLOBALS['fds'] as $v) {
            $server->push($v, 'pulltables'.json_encode($GLOBALS['hall']));
            if ($v == $fd3 || $v == $fd1 || $v == $fd2) {
                $server->push($v, 'freshorder'.json_encode([$GLOBALS['rooms'][$info->roomid],$GLOBALS['hall'][$info->roomid]]));
            }
        }
        $server->push($frame->fd, 'roomchat'.'{"nick":"系统消息","msg":"欢迎来到世界上最Low的21点对战游戏!"}');
    //改变准备状态
    } else if (substr($frame->data, 0, 15) == 'pushreadystatus') {
        // 等待 || 准备
        if (substr($frame->data, 15, 2) == 'ok') {
            $info = json_decode(substr($frame->data, 17));
            $GLOBALS['hall'][$info->roomid][$info->seatid.'_status'] = '准备';
            $GLOBALS['userlist'][$frame->fd]->status = '准备';
        } else if (substr($frame->data, 15, 2) == 'no') {
            $info = json_decode(substr($frame->data, 17));
            $GLOBALS['hall'][$info->roomid][$info->seatid.'_status'] = '等待';
            $GLOBALS['userlist'][$frame->fd]->status = '等待';
            $GLOBALS['hall'][$info->roomid]['status'] = 'off';
        }

        //刷新房间内信息变动
        if (isset($info)) {
            $fd1 = $GLOBALS['hall'][$info->roomid]['user_a'];
            $fd2 = $GLOBALS['hall'][$info->roomid]['user_b'];
            $fd3 = $GLOBALS['hall'][$info->roomid]['user_c'];
            //如果三个人状态都在准备中 推送开始游戏消息
            if ($GLOBALS['hall'][$info->roomid]['user_a_status'] == '准备' && 
                $GLOBALS['hall'][$info->roomid]['user_b_status'] == '准备' &&
                $GLOBALS['hall'][$info->roomid]['user_c_status'] == '准备') {
                //初始化房间数据
                resetRoom($info->roomid);
                //修改游戏状态 并且推送
                $GLOBALS['hall'][$info->roomid]['user_a_status'] = '游戏中';
                $GLOBALS['hall'][$info->roomid]['user_b_status'] = '游戏中';
                $GLOBALS['hall'][$info->roomid]['user_c_status'] = '游戏中';
                $GLOBALS['userlist'][$fd1]->status = '游戏中';
                $GLOBALS['userlist'][$fd2]->status = '游戏中';
                $GLOBALS['userlist'][$fd3]->status = '游戏中';
                $GLOBALS['hall'][$info->roomid]['status'] = 'on';
                $server->push($fd1, 'gostartgame'.json_encode([$GLOBALS['rooms'][$info->roomid],$GLOBALS['hall'][$info->roomid]]));
                $server->push($fd2, 'gostartgame'.json_encode([$GLOBALS['rooms'][$info->roomid],$GLOBALS['hall'][$info->roomid]]));
                $server->push($fd3, 'gostartgame'.json_encode([$GLOBALS['rooms'][$info->roomid],$GLOBALS['hall'][$info->roomid]]));
            }
            //大厅座位信息广播到全局 用户列表刷新
            foreach ($GLOBALS['rooms'][$info->roomid] as $v) {
                $server->push($v['fd'], 'freshorder'.json_encode([$GLOBALS['rooms'][$info->roomid],$GLOBALS['hall'][$info->roomid]]));
            }
        }

        //广播房间准备状态到全局
        foreach ($GLOBALS['fds'] as $v) {
            $server->push($v, 'pulllist'.json_encode($GLOBALS['userlist']));
            $server->push($v, 'pulltables'.json_encode($GLOBALS['hall']));
        }

    //广播房间内的消息
    } else if (substr($frame->data, 0, 8) == 'roomchat') {
        $info = json_decode(substr($frame->data, 8));
        foreach ($GLOBALS['rooms'][$info->roomid] as $v) {
            $server->push($v['fd'], 'roomchat'.substr($frame->data, 8));
        }

    //广播全局聊天
    } else if (substr($frame->data, 0, 9) == 'publicmsg') {
        foreach ($GLOBALS['fds'] as $v) {
            $server->push($v, $frame->data);
        }

    //接受确认游戏的信息并推送对局初始化    
    } else if (substr($frame->data, 0,8) == 'letsplay') {
        $info = json_decode(substr($frame->data, 8));
        //如果房间扑克牌数组是空的 那么初始化扑克牌数组
        if ($GLOBALS['hall'][$info->roomid]['pokers'] == []) {
            $GLOBALS['hall'][$info->roomid]['pokers'] = $GLOBALS['poker_list'];
        }
        //如果房间第一顺位是请求者 那么首先推送给他抽牌消息
        if ($GLOBALS['rooms'][$info->roomid][0]['fd'] == $frame->fd) {
            $GLOBALS['hall'][$info->roomid]['whosturn'] = 0;
            $server->push($frame->fd, 'getapoker');
            $waiting = 'waiting'.'{"nick":"'.$GLOBALS['rooms'][$info->roomid][0]['nick'].'"}';
            $server->push($GLOBALS['rooms'][$info->roomid][1]['fd'], $waiting);
            $server->push($GLOBALS['rooms'][$info->roomid][2]['fd'], $waiting);
        }
    //用户放弃抓牌事件
    } else if (substr($frame->data, 0,7) == 'wobuyao') {
        $info = json_decode(substr($frame->data, 7));

        //改变用户状态
        if ($GLOBALS['hall'][$info->roomid][$info->seatid.'_status'] != 'Burst') {
            $GLOBALS['hall'][$info->roomid][$info->seatid.'_status'] = '扣牌';
        }

        //拿到下一位
        $turn = $GLOBALS['hall'][$info->roomid]['whosturn'];

        //刷新出牌次序
        if ($GLOBALS['hall'][$info->roomid]['whosturn'] == count($GLOBALS['rooms'][$info->roomid]) - 1) {
            $GLOBALS['hall'][$info->roomid]['whosturn'] = 0;
        } else {
            $GLOBALS['hall'][$info->roomid]['whosturn'] = $GLOBALS['hall'][$info->roomid]['whosturn'] + 1;
        }

        //至少两个人扣牌了的时候 判断
        if (($GLOBALS['hall'][$info->roomid]['user_a_status'] == '扣牌' &&
             $GLOBALS['hall'][$info->roomid]['user_b_status'] == '扣牌') || 
            ($GLOBALS['hall'][$info->roomid]['user_b_status'] == '扣牌' && 
            $GLOBALS['hall'][$info->roomid]['user_c_status'] == '扣牌') || 
            ($GLOBALS['hall'][$info->roomid]['user_a_status'] == '扣牌' && 
            $GLOBALS['hall'][$info->roomid]['user_c_status'] == '扣牌')) {

            //如果三个人都扣牌了 开始结算 如果两个人扣牌了但是有一个人爆了 开始结算
            if ( ($GLOBALS['hall'][$info->roomid]['user_a_status'] == '扣牌' &&
                  $GLOBALS['hall'][$info->roomid]['user_b_status'] == '扣牌' &&
                  $GLOBALS['hall'][$info->roomid]['user_c_status'] == '扣牌')
                                            ||
                 ($GLOBALS['hall'][$info->roomid]['user_a_status'] == 'Burst' ||
                  $GLOBALS['hall'][$info->roomid]['user_b_status'] == 'Burst' || 
                  $GLOBALS['hall'][$info->roomid]['user_c_status'] == 'Burst') ) {

                //调用计分结算方法 返回win和lose数组
                $res = gameOver($info->roomid);

                //广播结算到整个房间 修改玩家状态
                foreach($GLOBALS['rooms'][$info->roomid] as $v) {
                    $GLOBALS['userlist'][$v['fd']]->status = '等待';
                    $server->push($v['fd'], 'gameover'.json_encode($res));
                    $server->push($v['fd'],'freshorder'.json_encode([$GLOBALS['rooms'][$info->roomid],$GLOBALS['hall'][$info->roomid]]));
                }

                //广播房间准备状态到全局
                foreach ($GLOBALS['fds'] as $v) {
                    $server->push($v, 'pulllist'.json_encode($GLOBALS['userlist']));
                    $server->push($v, 'pulltables'.json_encode($GLOBALS['hall']));
                }

            //否则继续下一位
            } else {
                //广播给下一位抽牌者
                $server->push($GLOBALS['rooms'][$info->roomid][$turn]['fd'], 'itsyourturn');
                //把下一位玩家广播给所有人 把不要牌的玩家广播给所有对手 
                $waiting = 'waiting'.'{"nick":"'.$GLOBALS['rooms'][$info->roomid][$turn]['nick'].'"}';
                foreach($GLOBALS['rooms'][$info->roomid] as $v) {
                    if ($v['fd'] != $GLOBALS['rooms'][$info->roomid][$turn]['fd']) {
                        $server->push($v['fd'], 'tabuyao'.'{"nick":"'.$GLOBALS['rooms'][$info->roomid][$turn]['nick'].'"}');
                    }
                    $server->push($v['fd'], $waiting);
                }
            }

        //下一位
        } else {
            //广播给下一位抽牌者
            $server->push($GLOBALS['rooms'][$info->roomid][$turn]['fd'], 'itsyourturn');
            //把下一位玩家广播给所有人 把不要牌的玩家广播给所有对手 
            $waiting = 'waiting'.'{"nick":"'.$GLOBALS['rooms'][$info->roomid][$turn]['nick'].'"}';
            foreach($GLOBALS['rooms'][$info->roomid] as $v) {
                if ($v['fd'] != $GLOBALS['rooms'][$info->roomid][$turn]['fd']) {
                    $server->push($v['fd'], 'tabuyao'.'{"nick":"'.$GLOBALS['rooms'][$info->roomid][$turn]['nick'].'"}');
                }
                $server->push($v['fd'], $waiting);
            }
        }

    //用户抽牌
    } else if (substr($frame->data, 0, 11) == 'chouyizhang') {
        $info = json_decode(substr($frame->data, 11));
        //取得扑克牌面
        $poker = array_rand($GLOBALS['hall'][$info->roomid]['pokers'], 1);
        //取得扑克的分值
        $value = $GLOBALS['poker_list'][$poker];
        //将牌发给该玩家
        $server->push($frame->fd, 'sendpoker'.'{"poker":"'.$poker.'","value":"'.$value.'"}');
        //记录这张牌
        $GLOBALS['hall'][$info->roomid][$info->seatid.'_pokers'][] = $poker;
        //从数组去掉这张牌
        unset($GLOBALS['hall'][$info->roomid]['pokers'][$poker]);

        //把用户抽牌的事件推送出去
        foreach($GLOBALS['rooms'][$info->roomid] as $v) {
            $server->push($v['fd'], 'hegotapoker'.'{"nick":"'.$info->nick.'"}');
        }

        //刷新出牌次序
        if ($GLOBALS['hall'][$info->roomid]['whosturn'] == count($GLOBALS['rooms'][$info->roomid]) - 1) {
            $GLOBALS['hall'][$info->roomid]['whosturn'] = 0;
        } else {
            $GLOBALS['hall'][$info->roomid]['whosturn'] = $GLOBALS['hall'][$info->roomid]['whosturn'] + 1;
        }

        //拿到下一位顺序
        $turn = $GLOBALS['hall'][$info->roomid]['whosturn'];
        //拿到下一位昵称
        $next = $GLOBALS['rooms'][$info->roomid][$turn]['nick'];
        //拿到下一轮玩家的昵称
        $waiting = 'waiting'.'{"nick":"'.$GLOBALS['rooms'][$info->roomid][$turn]['nick'].'"}';
         //计分
        $GLOBALS['hall'][$info->roomid][$info->seatid.'_score'] = $GLOBALS['hall'][$info->roomid][$info->seatid.'_score'] + $value;
        
        //如果超过21分 则爆牌 该玩家输了
        if ($GLOBALS['hall'][$info->roomid][$info->seatid.'_score'] > 21) {
            //分数为负9999
            $GLOBALS['hall'][$info->roomid][$info->seatid.'_score'] = -9999;
            //玩家状态为burst
            $GLOBALS['hall'][$info->roomid][$info->seatid.'_status'] = 'Burst';
            //告诉玩家他输了
            $server->push($frame->fd, 'scoreburst');
            //把该玩家输了的消息推送给整个房间
            foreach ($GLOBALS['rooms'][$info->roomid] as $v) {
                $server->push($v['fd'], 'oneloose'.'{"nick":"'.$info->nick.'"}');
            }
            //如果有两个人Burst
            if (($GLOBALS['hall'][$info->roomid]['user_a_status'] == 'Burst' &&
                 $GLOBALS['hall'][$info->roomid]['user_b_status'] == 'Burst') || 
                ($GLOBALS['hall'][$info->roomid]['user_b_status'] == 'Burst' && 
                $GLOBALS['hall'][$info->roomid]['user_c_status'] == 'Burst') || 
                ($GLOBALS['hall'][$info->roomid]['user_a_status'] == 'Burst' && 
                $GLOBALS['hall'][$info->roomid]['user_c_status'] == 'Burst')) {
                $res = gameOver($info->roomid);
                //游戏结束消息广播到整个房间 更新用户状态
                foreach ($GLOBALS['rooms'][$info->roomid] as $v) {
                    $GLOBALS['userlist'][$v['fd']]->status = '等待';
                    $server->push($v['fd'], 'gameover'.json_encode($res));
                    $server->push($v['fd'], 'freshorder'.json_encode([$GLOBALS['rooms'][$info->roomid],$GLOBALS['hall'][$info->roomid]]));
                }
                //广播房间准备状态到全局
                foreach ($GLOBALS['fds'] as $v) {
                    $server->push($v, 'pulllist'.json_encode($GLOBALS['userlist']));
                    $server->push($v, 'pulltables'.json_encode($GLOBALS['hall']));
                }
            } else {
                //让下一位玩家抽牌
                $server->push($GLOBALS['rooms'][$info->roomid][$turn]['fd'], 'itsyourturn');
                //把下一位玩家广播给房间内的所有人
                foreach($GLOBALS['rooms'][$info->roomid] as $v) {
                    $server->push($v['fd'], $waiting);
                }
            }

        //如果用户拿到了21点
        } else if ($GLOBALS['hall'][$info->roomid][$info->seatid.'_score'] == 21) {
            //游戏结束消息广播到整个房间 用户列表刷新
            $res = gameOver($info->roomid);
            foreach ($GLOBALS['rooms'][$info->roomid] as $v) {
                $GLOBALS['userlist'][$v['fd']]->status = '等待';
                $server->push($v['fd'], 'gameover'.json_encode($res));
                $server->push($v['fd'], 'freshorder'.json_encode([$GLOBALS['rooms'][$info->roomid],$GLOBALS['hall'][$info->roomid]]));
            }
            //广播房间准备状态到全局
            foreach ($GLOBALS['fds'] as $v) {
                $server->push($v, 'pulllist'.json_encode($GLOBALS['userlist']));
                $server->push($v, 'pulltables'.json_encode($GLOBALS['hall']));
            }
        } else {
            //让下一位玩家抽牌
            $server->push($GLOBALS['rooms'][$info->roomid][$turn]['fd'], 'itsyourturn');
            //把下一位玩家广播给房间内的所有人
            foreach($GLOBALS['rooms'][$info->roomid] as $v) {
                $GLOBALS['userlist'][$v['fd']]->status = '等待';
                $server->push($v['fd'], $waiting);
            }
        }
    
    } else {

        //预留
    }

});